package com.allen.admin.service.server.manager.impl;

import com.allen.admin.service.common.domain.AdminRolePerm;
import com.xxx.dao.GenericDao;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class AdminRolePermManager {

	@Autowired
    private GenericDao dao;

	public AdminRolePerm insert(AdminRolePerm obj) {
		
		if (obj == null) {
			return null;
		}
		
		int affected = dao.insertAndReturnAffectedCount("AdminRolePermMapper.insert", obj);
		
		if (affected > 0) {
			return obj;
		} else {
			return null;
		}
	}
	
	public int delete(Integer roleId) {
		
		if (roleId == null) {
			return 0;
		}

		int affected = dao.updateByObj("AdminRolePermMapper.delete", roleId);
		return affected;

	}

	public List<AdminRolePerm> select(Integer roleId) {
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("roleId", roleId);
		
		List<AdminRolePerm> list = dao.queryList("AdminRolePermMapper.select", map);
		return list;
	}
	
	public List<String> selectCodesByRoles(List<Integer> roleIds) {
		
		if (CollectionUtils.isEmpty(roleIds)) {
			return new ArrayList<String>();
		}
		
		Map<String, Object> map = new HashMap<String, Object>();
		if (roleIds.size() == 1) {
			map.put("roleId", roleIds.get(0));
		} else {
			map.put("roleIds", roleIds);
		}
		
		
		List<String> list = dao.queryList("AdminRolePermMapper.selectCodesByRoles", map);
		return list;
	}
	
}
